package com.mb.app.controller;

import com.mb.app.domain.TbIndexClassCfg;
import com.mb.app.domain.TbIndexClassType;
import com.mb.app.domain.TbIndexProductInfo;
import com.mb.app.service.ITbIndexClassCfgService;
import com.mb.app.service.ITbIndexClassTypeService;
import com.mb.app.service.ITbIndexProductInfoService;
import com.mb.common.controller.BaseController;
import com.mb.common.utils.DateUtils;
import com.mb.common.utils.PageUtils;
import com.mb.common.utils.Query;
import com.mb.common.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 首页商品信息  前端控制器
 * </p>
 *
 * @author zwl
 * @since 2019-04-03
 */
@Controller
@RequestMapping("/app/indexProduct")
@Slf4j
public class TbIndexProductInfoController extends BaseController {

    @Autowired
    private ITbIndexProductInfoService indexProductInfoService;

    @Autowired
    private ITbIndexClassCfgService indexClassCfgService;

    @Autowired
    private ITbIndexClassTypeService indexClassTypeService;

    @GetMapping("/index/{classType}")
    String index(Model model,@PathVariable("classType") String classType){
        TbIndexClassType indexClassType = indexClassTypeService.getDtl(Long.parseLong(classType));
        if (indexClassType != null) {
            model.addAttribute("indexClassId",indexClassType.getClassTypeId());
        }
        return "/app/indexProduct/indexProduct_list";
    }


    @ResponseBody
    @GetMapping("/list")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        // 查询列表数据
        // 时间截取
        String createTime = (String) params.get("createTime");
        if (StringUtils.isNotEmpty(createTime)) {
            params.put("startTime", DateUtils.parse(createTime.substring(0,10),DateUtils.DATE_PATTERN));
            params.put("endTime",createTime.substring(13,23)+" 23:59:59");
        }
        Query query = new Query(params);
        List<TbIndexProductInfo> list = indexProductInfoService.queryList(query);
        int total = indexProductInfoService.count(query);
        PageUtils pageUtils = new PageUtils(list, total);
        return pageUtils;
    }

    @GetMapping("/add/{indexClassId}")
    String add(Model model,@PathVariable(value = "indexClassId") Long indexClassId){
        model.addAttribute("indexClassId",indexClassId);
        return "/app/indexProduct/indexProduct_add";
    }


    @ResponseBody
    @PostMapping("/save")
    public R save(TbIndexProductInfo TbIndexProductInfo, HttpServletRequest request){
        if (TbIndexProductInfo.getProductIds().length == 0){
            TbIndexProductInfo.setAddUserId(getUserId());
            //默认值
            TbIndexProductInfo.setClassTypeId((long)0);
            int result = indexProductInfoService.save(TbIndexProductInfo);
            return R.ok();
        }else {
            List<TbIndexProductInfo> indexProductInfos = new ArrayList<>();
            for (Long productId:TbIndexProductInfo.getProductIds()) {
                TbIndexProductInfo indexProductInfo = new TbIndexProductInfo();
                indexProductInfo.setIndexClassId(TbIndexProductInfo.getIndexClassId());
                indexProductInfo.setAddUserId(getUserId());
                indexProductInfo.setProductId(productId);
                indexProductInfos.add(indexProductInfo);
                //默认值
                indexProductInfo.setClassTypeId((long)0);
            }
            indexProductInfoService.insertBatch(indexProductInfos);
            return R.ok();
        }
    }

    @ResponseBody
    @PostMapping("/remove")
    public R remove(@RequestParam("id") Long id, HttpServletRequest request){
        Long delUserId = getUserId();
        int result = indexProductInfoService.remove(id,delUserId);
        return R.ok();
    }

    @GetMapping("/dtl/{id}")
    String dtl(@PathVariable("id") Long id, Model model){
        model.addAttribute("indexProductInfo",indexProductInfoService.getDtl(id));
        return "/app/indexProduct/indexProduct_info";
    }


    @GetMapping("/edit/{id}")
    String edit(@PathVariable("id") Long id, Model model){
        model.addAttribute("indexProductInfo",indexProductInfoService.getDtl(id));
        // 获取首页商品分类
        Map<String,Object> indexClassMap = new HashMap<String, Object>();
        List<TbIndexClassCfg> indexClassCfgs = indexClassCfgService.selectByMap(indexClassMap);
        model.addAttribute("indexClassCfgs",indexClassCfgs);
        return "/app/indexProduct/indexProduct_edit";
    }


    @ResponseBody
    @PostMapping("/update")
    public R update(TbIndexProductInfo TbIndexProductInfo, HttpServletRequest request){
        TbIndexProductInfo.setUpdateUserId(getUserId());
        int result = indexProductInfoService.update(TbIndexProductInfo);
        return R.ok();
    }

    /**
     * 首页商品名称是否存在
     */
    @PostMapping("/indexProductNameExit")
    @ResponseBody
    boolean indexProductsNameExit(@RequestParam Map<String, Object> params) {
        // 存在，不通过，false
        return !indexProductInfoService.indexProductNameExit(params);
    }
}
